package com.skydroid.cv_demo;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Administrator on 2017/8/14.
 */
public class DetectionTargetView extends View {
    private int w;
    private int h;
    private int downX;
    private int downY;
    private int upX;
    private int upY;
    private Paint paint,paint2;
    private Rect r1;
    private boolean isCanUpdate = false;
    private boolean isCanShow = false;
    public OnConfirmTargetCallBack onConfirmTargetCallBack;

    public interface OnConfirmTargetCallBack {
        void onClearTarget();
        void onConfirmTarget(int viewWidth,int viewHeight,Rect target);
    }

    public DetectionTargetView(Context context) {
        super(context);
        init();
    }

    public DetectionTargetView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DetectionTargetView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.GREEN);                    //设置画笔颜色
        paint.setStrokeWidth((float) 5.0);              //线宽
        paint.setStyle(Paint.Style.STROKE);                   //空心效果

        paint2 = new Paint();
        paint2.setAntiAlias(true);
        paint2.setColor(Color.GREEN);                    //设置画笔颜色
        paint2.setTextSize((float) 20.0);
        paint2.setStyle(Paint.Style.STROKE);                   //空心效果

        r1 = new Rect();                         //Rect对象
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        isCanUpdate = false;
//        Log.e("touch", "x:" + event.getX() + "; y:" + event.getY());
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isCanShow = false;
                upX = 0;
                upY = 0;
                downX = (int) event.getX();
                downY = (int) event.getY();
                clearTarget();
                break;
            case MotionEvent.ACTION_MOVE:
                isCanShow = true;
                upX = (int) event.getX();
                upY = (int) event.getY();
                w = upX - downX;
                h = upY - downY;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                isCanShow = true;
                upX = (int) event.getX();
                upY = (int) event.getY();
                w = upX - downX;
                h = upY - downY;
                isCanUpdate = true;
                invalidate();
                OnConfirmTargetCallBack drawCallBack2 = this.onConfirmTargetCallBack;
                if (drawCallBack2 != null){
                    drawCallBack2.onConfirmTarget(getMeasuredWidth(),getMeasuredHeight(),getTargetRect());
                }
                break;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isCanShow) {
            canvas.drawColor(Color.parseColor("#00000000"));
            if (w > 0){
                r1.left = downX;
                r1.right = downX + w;
            }else {
                r1.left = upX;
                r1.right = upX - w;
            }
            if (h > 0){
                r1.top = downY;
                r1.bottom = downY + h;
            }else {
                r1.top = upY;
                r1.bottom = upY - h;
            }
            canvas.drawRect(r1, paint);                 //绘制矩形
//            if (text != null) {
//                canvas.drawText(text, x, y + h + 20, paint2);
//            }
        }else {
            canvas.drawColor(Color.parseColor("#00000000"));
        }

    }

    public void updateRect(Rect rect){
        if (isCanUpdate){
            upX = rect.right;
            upY = rect.bottom;
            downX = rect.left;
            downY = rect.top;
            w = rect.width();
            h = rect.height();
            invalidate();
        }
    }

    public Rect getTargetRect(){
        return new Rect(r1.left,r1.top,r1.right,r1.bottom);
    }

    public void clearTarget(){
        isCanUpdate = false;
        isCanShow = false;
        OnConfirmTargetCallBack drawCallBack1 = this.onConfirmTargetCallBack;
        if (drawCallBack1 != null){
            drawCallBack1.onClearTarget();
        }
        invalidate();
    }

    public void setConfirmTargetCallBack(OnConfirmTargetCallBack onConfirmTargetCallBack) {
        this.onConfirmTargetCallBack = onConfirmTargetCallBack;
    }

}
